<?php
require_once ('Controller/Mobile/Common.inc.php');
require_once ('View/Result.inc.php');
#require_once ('Model/DeviceModel.inc.php');
final class Controller extends common {
	private $Result;
	private $Loader;
	private $Callback = NULL;
	public function __construct () {
		parent::__construct();
		$trace = debug_backtrace();
		if (isset ($trace[1])) {
			$this->Loader = $trace[1]['object'];
		}
	}

	public function execude () {
		header ('Content-Type: text/xml');
		$this->Result = new Result ();
		if (array_key_exists ('action', $_REQUEST)) {
			$this->getFunction ($_REQUEST['action']);
		} else {
			$this->defaultPage ();
		}
		print $this->Result->Create ($this->Callback);
	}

	public function getFunction ($action) {
		switch ($action) {
			default:
				$this->defaultPage ();
				break;
		}
	}

	public function defaultPage () {
		$this->Result->setModulename (sprintf ('M%sF%s', $this->Loader->module, __FUNCTION__));
		if (array_key_exists ('room', $_REQUEST) && $_REQUEST['room'] != '' && array_key_exists ('id', $_REQUEST) && ctype_digit ($_REQUEST['id']) && array_key_exists ('startDate', $_REQUEST) && $_REQUEST['endDate'] != '') {
#print $_REQUEST['endDate'];
#print $_REQUEST['startDate'];
			$startHoure = 13;
			$startMinute = 00;
			$startSecond = 00;
			$startDay = substr ($_REQUEST['startDate'], 0, 2);
			$startMonth = substr ($_REQUEST['startDate'], 2, 2);
			$startYear = substr ($_REQUEST['startDate'], 4, 4);
			$startTime = mktime ($startHoure, $startMinute, $startSecond, $startMonth, $startDay, $startYear);

			$endHoure = 12;
			$endMinute = 00;
			$endSecond = 00;
			$endDay = substr ($_REQUEST['endDate'], 0, 2);
			$endMonth = substr ($_REQUEST['endDate'], 2, 2);
			$endYear = substr ($_REQUEST['endDate'], 4, 4);
			$endTime = mktime ($endHoure, $endMinute, $endSecond, $endMonth, $endDay, $endYear);
			$now = time ();
			if ($now > $endTime || $startTime > $endTime || $now < $startTime) {
				$this->Result->setErrorCode (4); // Card date expired
			} else {
				$DeviceInfo = $this->DeviceModel->CheckDeviceItemExistByID ($_REQUEST['id']);
				if ($DeviceInfo != NULL) {
					$CircleInfo = $this->CircleModel->CheckCircleItemExistByName ($_REQUEST['room']);
					if ($CircleInfo != NULL) {
						$CirclesInfo = $this->DeviceToCircleModel->GetCirclesByDeviceCount ($DeviceInfo[$this->DeviceModel->idRowName]);
						if ($CirclesInfo == 0) {
							$this->DeviceToCircleModel->AddDeviceToCircle ($DeviceInfo[$this->DeviceModel->idRowName], $CircleInfo[$this->CircleModel->idRowName], date ('Y-m-d H:i:s', $startTime), date ('Y-m-d H:i:s', $endTime));
							$this->Result->setStatus (200);
							$this->Result->setErrorCode (0); // Circle not exist
						} else {
							$CirclesInfo = $this->DeviceToCircleModel->GetCirclesByDevice ($DeviceInfo[$this->DeviceModel->idRowName]);
							if ($CirclesInfo[0][$this->DeviceToCircleModel->circleIDRowName] == $CircleInfo[$this->CircleModel->idRowName]) {
								$this->DeviceToCircleModel->UpdateDeviceToCircle ($CirclesInfo[0][$this->DeviceToCircleModel->idRowName], $CirclesInfo[0][$this->DeviceToCircleModel->deviceIDRowName], $CirclesInfo[0][$this->DeviceToCircleModel->circleIDRowName], date ('Y-m-d H:i:s', $startTime), date ('Y-m-d H:i:s', $endTime));
								$this->Result->setStatus (200);
								$this->Result->setErrorCode (0);
							} else {
								$this->Result->setErrorCode (2); // Device ID was mapped
							}
						}
					} else {
						$this->Result->setErrorCode (3); // Circle not exist
					}
				} else {
					$this->Result->setErrorCode (1); // Device ID not exist
				}
			}
		}
	}

/*	private $DeviceID = 0;
	public function getDeviceIDPage () {
		$this->Result->setModulename (sprintf ('M%sF%s', $this->Loader->module, __FUNCTION__));
		if (array_key_exists ('IMEI', $_REQUEST) && $_REQUEST['IMEI'] != '' && array_key_exists ('PhoneNumber', $_REQUEST) && $_REQUEST['PhoneNumber'] != '' && array_key_exists ('SimSerialNumber', $_REQUEST) && $_REQUEST['SimSerialNumber'] != '' && array_key_exists ('IMSI', $_REQUEST) && $_REQUEST['IMSI'] != '' && array_key_exists ('Manufacturer', $_REQUEST) && $_REQUEST['Manufacturer'] != '' && array_key_exists ('Model', $_REQUEST) && $_REQUEST['Model'] != '' && array_key_exists ('OS', $_REQUEST) && $_REQUEST['OS'] != '' && array_key_exists ('Version', $_REQUEST) && $_REQUEST['Version'] != '') {
			$this->Result->setStatus (200);
			$this->Result->setErrorCode (0);
			if (($this->DeviceID = $this->DeviceModel->CheckDeviceItemExist ($_REQUEST['IMEI'])) == NULL) {
				$this->DeviceID = $this->DeviceModel->AddDeviceItem ($_REQUEST['IMEI'], $_REQUEST['PhoneNumber'], $_REQUEST['SimSerialNumber'], $_REQUEST['IMSI'], $_REQUEST['Manufacturer'], $_REQUEST['Model'], $_REQUEST['OS'], $_REQUEST['Version'], $Remark = '');
			} else {
				$this->DeviceID = $this->DeviceID[$this->DeviceModel->idRowName];
			}
			$this->Callback = array ($this, 'getDeviceIDPageCallBack');
		}
	}*/
/*	private $ErrorCode = 0;
	public function getRegisterPageCallBack ($dom, $result) {
#		$result->setAttribute ('DeviceID', $this->DeviceID);
		$this->Result->setErrorCode ($this->ErrorCode);
	}*/

/*	public function Post () {
		$this->Result->setModulename (sprintf ('M%sF%s', $this->Loader->module, __FUNCTION__));
		if (array_key_exists ('title', $_REQUEST) && $_REQUEST['title'] != '' && array_key_exists ('description', $_REQUEST) && $_REQUEST['description'] != '') {
			$this->Result->setStatus (200);
			$this->Result->setErrorCode (0);
		} else {
			$this->Result->setErrorCode (1);
		}
	}*/
}
?>
